"""
Case Type   : 小型化
Case Name   : gsql 获取帮助信息
Create At   : 2025/1/22
Owner       : wan005
Description :
    1.gsql 获取帮助信息:gsql -?
Expect      :
    1.有--pipeline参数说明
History     :
"""
import os
import time
import unittest
from testcase.utils.ComInstall import LiteInstall, LiteUnInstall
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Lite(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.com_install = LiteInstall(self.pkg_path, None, None)
        self.com_uninstall = LiteUnInstall(self.pkg_path)
        self.root_node = Node('PrimaryRoot')
        self.user_node = Node('PrimaryDbUser')
        self.user = None
        self.port = None

    def test_audit(self):
        text = '----prestep1:获取可用用户和端口;expect:获取成功----'
        self.log.info(text)
        self.user, self.port = self.com_install.get_user_and_port(self.root_node)
        self.log.info(f'self.user:{self.user}, self.port:{self.port}')
        self.user_node.ssh_user = self.user
        self.user_node.ssh_password = macro.COMMON_PASSWD
        self.data_path = os.path.join('/home', self.user, 'data')
        self.app_path = os.path.join('/home', self.user, 'app')
        self.audit_path = os.path.join(self.data_path, 'pg_audit')
        self.com_install.data_path = self.data_path
        self.com_install.app_path = self.app_path

        text = '----prestep2:创建数据库用户和用户组;expect:创建成功----'
        self.log.info(text)
        self.com_install.prepare_user(self.user, self.root_node)

        text = '----prestep3:获取数据库安装包并解压;expect:成功----'
        self.log.info(text)
        res = self.com_install.tar_pkg(self.root_node)
        self.assertIn('install.sh', res, f'执行失败: {text}')

        text = '----prestep4:执行脚本安装数据库;expect:安装成功----'
        self.log.info(text)
        res = self.com_install.install_single(self.user_node, self.port)
        self.log.info(res)
        self.assertIn('start datanode success', res, f'执行失败: {text}')

        text = '----step1:执行查看帮助信息命令;except:显示详细的帮助信息----'
        self.log.info(text)
        dump_cmd = f'gsql -?'
        dump_msg = self.user_node.sh(dump_cmd).result()
        self.log.info(dump_msg)
        params = ['-c, --command=COMMAND', '-d, --dbname=DBNAME',
                  '-f, --file=FILENAME', '-l, --list',
                  '-v, --set=, --variable=NAME=VALUE', '-V, --version',
                  '-X, --no-gsqlrc', '-1 ("one"), --single-transaction',
                  '-?, --help', '-a, --echo-all', '-e, --echo-queries',
                  '-E, --echo-hidden', '-k, --with-key=KEY',
                  '-L, --log-file=FILENAME', '-m, --maintenance',
                  '-n, --no-libedit', '-o, --output=FILENAME', '-q, --quiet',
                  '-C, --enable-client-encryption', '-s, --single-step',
                  '-S, --single-line', '-A, --no-align',
                  '-F, --field-separator=STRING', '-H, --html',
                  '-P, --pset=VAR[=ARG]', '-R, --record-separator=STRING',
                  '-r', '-t, --tuples-only', '-T, --table-attr=TEXT',
                  '-x, --expanded', '-z, --field-separator-zero',
                  '-0, --record-separator-zero', '-2, --pipeline',
                  '-h, --host=HOSTNAME', '-p, --port=PORT',
                  '-U, --username=USERNAME', '-W, --password=PASSWORD']
        result = all(set(i).issubset(dump_msg) for i in params)
        self.assertEqual(result, True, f'执行失败: {text}')

    def tearDown(self):
        text = '-----清理环境-----'
        self.log.info(text)
        res = self.com_uninstall.uninstall(self.user_node)
        self.com_uninstall.clear_env(self.root_node)
        self.assertIn('success', res, f'执行失败: {text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
